package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.CreditCradType;

public class CreditCardTypeLogic extends AbstractBL{
	public List<CreditCradType> GenerateCreditCradType()
	{
		List<CreditCradType> list = new ArrayList<CreditCradType>();
		
		CreditCradType creditCardtype;
		if(GetCreditCradType2("VISA")==null){
			creditCardtype= new CreditCradType();
			creditCardtype.setCardTypeName("VISA");
			creditCardtype.setStatus(true);
			list.add(creditCardtype);
		}
		if(GetCreditCradType2("MasterCard")==null){
			creditCardtype = new CreditCradType();
			creditCardtype.setCardTypeName("MasterCard");
			creditCardtype.setStatus(true);
			list.add(creditCardtype);
		}
		return list;
	}
	
	public CreditCradType GetCreditCradType(long l)
	{
		Key key = KeyFactory.createKey(EntityName.CreditCardType, l);
		try {
			Entity lang = datastore.get(key);
			CreditCradType c = new CreditCradType();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
		
	public CreditCradType GetCreditCradType2(String title)
	{
		Query q = new Query(EntityName.CreditCardType);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterTitle = new FilterPredicate("CardTypeName",FilterOperator.EQUAL,title);
		Filter filter = CompositeFilterOperator.and(filterStatus,filterTitle);
		q.setFilter(filter);
		return GetCreditCradType(datastore.prepare(q).asSingleEntity());
	}
	
	public List<CreditCradType> GetAllCreditCradType()
	{
		Query q = new Query(EntityName.CreditCardType);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		q.setFilter(filterStatus);
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public CreditCradType GetCreditCradType(Entity entity)
	{
		if(entity == null) return null;
		CreditCradType item = new CreditCradType();
		item.setEntity(entity);
		return item;
	}
	
	public List<CreditCradType> GetList(List<Entity> entities)
	{
		List<CreditCradType> list = new ArrayList<CreditCradType>();
		for(Entity e : entities)
		{
			CreditCradType a = new CreditCradType();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}

	public CreditCradType AddCreditCradType(String title) throws EntityNotFoundException{
		CreditCradType t = new CreditCradType();
		t.setCardTypeName(title);
		t.setStatus(true);
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);
		return t;
	}
	
	public CreditCradType DeleteCreditCradType(long CCtypeId) throws EntityNotFoundException{
		CreditCradType t = GetCreditCradType(CCtypeId);
		t.setStatus(false);
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);
		return t;
	}
	
	public CreditCradType ChangeStatus(long CCTypeId) throws EntityNotFoundException{
		CreditCradType a = GetCreditCradType(CCTypeId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);
		return a;
	}
}
